package com.enseirb.bdxmap.dao.webrequest;

import java.net.InetAddress;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

	private Context context;
	private final String TRUSTWORTHY_HOST = "google.com";
	
	public ConnectionDetector(Context context){
		this.context = context;
	}

	public boolean isNetworkConnected(){
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) 
		{
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) 
				for (int i = 0; i < info.length; i++) 
					if (info[i].getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}

		}
		return false;
	}

	public boolean isInternetAvailable() {
		try {
			InetAddress ipAddr = InetAddress.getByName(TRUSTWORTHY_HOST); 

			if (ipAddr.equals("")) {
				return false;
			} else {
				return true;
			}

		} catch (Exception e) {
			return false;
		}

	}

	public boolean isConnected(){
		return context != null ? ((isInternetAvailable()) && (isNetworkConnected())) : false;
	}

}
